package com.dnzx.socket;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.dnzx.util.ByteUtil;

import java.io.*;

@Service
public class ServerHandler implements MessageHandler {

    private static final Logger LOGGER = LoggerFactory.getLogger(ServerHandler.class);
    private static final byte[] END = new byte[]{0x0d, 0x0a};
    private static final byte[] HEAD = new byte[]{(byte) 0xBB};
    private static final byte[] GET_EPC = new byte[]{(byte) 0xBB, 0x19, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x06, 0x2E, 0x0D, 0x0A};
    private DataInputStream dis = null;
    private DataOutputStream dos = null;

    @Override
    public void init(OutputStream os, InputStream is) {
        dis = new DataInputStream(is);
        dos = new DataOutputStream(os);
    }

    @Override
    public void onReceive() {
        byte[] header = new byte[3];
        byte[] responsemsg = null;
        try {
            dis.read(header);
            String head = ByteUtil.byte2hex(ByteUtil.subByte(header, 0, 1));
            if (!head.equals("BB")) {
                dos.write(END);
                return;
            }
            String type = ByteUtil.byte2hex(ByteUtil.subByte(header, 1, 1));
            System.out.println(type);
            int len = ByteUtil.byte2Int(header, 2, 1);
            byte[] inputs = new byte[len];
            dis.read(inputs);
            if (null != type) {
                switch (type) {
                    case "96"://查询标签数据响应
                    case "9A"://写入标签数据响应
                    default:
                        break;
                }
            }
            dos.write(END);
            // dos.write(responseMessage(pacage, responsemsg));
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("", e);
        }
    }

    @Override
    public void onReceive(OutputStream os, InputStream is, boolean getEpc) {
        dis = new DataInputStream(is);
        dos = new DataOutputStream(os);
        byte[] header = new byte[3];
        byte[] responsemsg = null;
        try {
            if (getEpc) {
                os.write(GET_EPC);
                os.flush();
                System.out.println("111111");
            }

                is.read(header);
                String head = ByteUtil.byte2hex(ByteUtil.subByte(header, 0, 1));
                if (!head.equals("BB")) {
                    os.write(END);
                    return;
                }
                String type = ByteUtil.byte2hex(ByteUtil.subByte(header, 1, 1));
                System.out.println(type);
                int len = ByteUtil.byte2Int(header, 2, 1);
                byte[] inputs = new byte[len];
                is.read(inputs);
                if (null != type) {
                    switch (type) {
                        case "96"://查询标签数据响应
                        case "9A"://写入标签数据响应
                        default:
                            break;
                    }
                }
                os.write(END);
                os.flush();



        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("", e);

        }
    }


    private byte[] responseMessage(byte[] input1, byte[] input2) {
        byte[] context = ByteUtil.concatAll(input1, input2, END);
        byte lrc = ByteUtil.byteXOR(context);
        byte[] reqlen = ByteUtil.intToByte2(context.length + 1);
        return null;
    }
}
